
import axios from 'axios'
import Vue from 'vue'
import {OPEN_AUTH_URL} from '@/constants'
import { JAVAHOST } from '@/constants'
import {storage} from '@/utils/tools'
import {  Loading } from 'element-ui';

const Axios = axios.create({
  baseURL:JAVAHOST,//http://192.168.1.210:16001
  withCredentials:true,
  responseType: 'json', // default
  transformResponse: [function (data) {
    return data
  }],
  timeout: 30000
})



//http://192.168.69.114:16401/knowledge/knowledgeTypes

var loading_ui = null;
var loadingQueue = [];
window.showLoading = function(title='') {
  if(loadingQueue.length == 0) {
    loading_ui = Loading.service({
      lock: true,
      text: title?title:'请稍等...',
      spinner: 'el-icon-loading',
      background: 'rgba(255, 255, 255, 0.4)'
    });
  }
  loadingQueue.push("loading");
};
window.hideLoading = function() {
  loadingQueue.pop();
  if(loadingQueue.length == 0) {
    Vue.nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
      loading_ui.close();
    });

  }
};


Axios.interceptors.request.use(config => {

  config.headers['token'] = localStorage.getItem('token') || ''
   console.log(config)
  // config.params.outMembersId = localStorage.getItem('out_member_id') || ''
// alert(localStorage.getItem('wxUserId') )

  var x = localStorage.getItem('wxUserId') || ''
  if(x){
    if(config.method == 'post') {
      if(!config.data) {
        config.data = {wxUserId: ''}
      }
      config.data.wxUserId = localStorage.getItem('wxUserId') || ''
    }
  }


  if(config.method == 'get') {
    var wxu = localStorage.getItem('wxUserId') || ''
    if(config.params){
      config.params.wxUserId=localStorage.getItem('wxUserId') || ''
    }else{
      config.params={
        wxUserId:localStorage.getItem('wxUserId') || ''
      }
    }

  }
// debugger;
  showLoading();

  return config
}, error => {
  setTimeout(()=>{
    // Indicator.close()
    hideLoading();
  },500)
  return Promise.reject(error)
})


Axios.interceptors.response.use(response => {

  if(response.status == '200') {
    response = response.data;
  setTimeout(()=>{
    // Indicator.close()
    hideLoading();
},500)
  } else {
    showLoading('网络异常，请稍后重试！')
    setTimeout(()=>{
      hideLoading();
    },500)
  }

  return Promise.resolve(response)
}, error => {
  setTimeout(()=>{
    // Indicator.close()
    hideLoading();
  },500)
  return Promise.reject(error)
})


export default Axios
